package com.eastfair.venueservice.service.impl;

import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.transport.api.TransportRegisterFeign;
import com.eastfair.transport.dto.BindServiceProviderRoleDTO;
import com.eastfair.transport.dto.RemoveServiceProviderRoleDTO;
import com.eastfair.venueservice.entity.ServiceProvider;
import com.eastfair.venueservice.enumeration.ServiceProviderRoleEnum;
import com.eastfair.venueservice.service.VenueManageTransportRegisterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 主场注册服务.
 *
 * @author dqq
 * @date 2022 /8/10
 */
@Service
@Slf4j
public class VenueManageTransportRegisterServiceImpl implements VenueManageTransportRegisterService {

    @Resource
    private TransportRegisterFeign transportRegisterFeign;

    @Override
    public boolean bindServiceProviderRole(BindServiceProviderRoleDTO bindHomeServiceProviderRoleDTO) {
        R<Boolean> result = transportRegisterFeign.bindServiceProviderRole(bindHomeServiceProviderRoleDTO);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public boolean bindServiceProviderRole(Long userId, Long serviceProviderId, String serviceProviderName, String serviceProviderRole) {
        BindServiceProviderRoleDTO roleDTO = new BindServiceProviderRoleDTO();
        roleDTO.setServiceProviderRole(serviceProviderRole);
        roleDTO.setUserId(userId);
        roleDTO.setServiceProviderId(serviceProviderId);
        roleDTO.setServiceProviderName(serviceProviderName);
        return bindServiceProviderRole(roleDTO);
    }

    @Override
    public boolean removeServiceProviderRole(List<RemoveServiceProviderRoleDTO> removeHomeServiceProviderRoleDTOList) {
        R<Boolean> result = transportRegisterFeign.removeServiceProviderRole(removeHomeServiceProviderRoleDTOList);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public boolean removeRole(List<ServiceProvider> list, ServiceProviderRoleEnum serviceProviderRoleEnum) {
        List<RemoveServiceProviderRoleDTO> roleList = list
                .stream()
                .map(serviceProvider -> RemoveServiceProviderRoleDTO
                        .builder()
                        .serviceProviderId(serviceProvider.getId())
                        .userId(serviceProvider.getUserId())
                        .serviceProviderRole(serviceProviderRoleEnum.getCode())
                        .build())
                .collect(Collectors.toList());
        return removeServiceProviderRole(roleList);
    }
}